package com.itheima.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;

import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;
    @RequestMapping("/setNumberByDate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting){
    orderSettingService.setNumberByDate(orderSetting);
    return Result.success("");
    }
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> dataArray = POIUtils.readExcel(excelFile);
            if(CollectionUtil.isNotEmpty(dataArray)){
                ArrayList<OrderSetting> orderSettings = new ArrayList<>();
                //把dataArray转为List<OrderSetting>类型的数据传递到Service,也可以在Service里面做
                for (String[] row : dataArray) {//一行数据
                    if(row.length!=2){
                        continue;
                    }
                    String dateStr=row[0];
                    String numberStr=row[1];
                    DateTime orderDate = DateUtil.parse(dateStr, "yyyy/MM/dd");
                    Integer number = Integer.valueOf(numberStr);
                    OrderSetting orderSetting = new OrderSetting(orderDate,number);
                    orderSettings.add(orderSetting);
                }
                orderSettingService.add(orderSettings);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success("");
    }
    @RequestMapping("/findByDate")
    public Result findByDate(String date){
        //返回结果必须如下[{"date":22,"month":1,"number":300,"reservations":300,}]
       List<Map> maps=orderSettingService.findByDate(date);
       return Result.success("",maps);
    }
}
